package com.test.service.Impl;

import com.test.Aspect.BusinessException;
import com.test.dao.StoreCanteenMapper;
import com.test.domain.StoreCanteen;
import com.test.domain.StoreCanteenExample;
import com.test.enums.Page;
import com.test.enums.Replay;
import com.test.enums.ReplayCode;
import com.test.service.StoreCanteenService;
import com.test.utils.ReplayUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * StoreCanteetServiceImpl
 *
 * @author HY
 * 2021/05/24 8:45
 **/

@Service
public class StoreCanteetServiceImpl implements StoreCanteenService {

    @Resource
    private StoreCanteenMapper storeCanteenMapper;

    @Override
    public Replay insert(StoreCanteen storeCanteen) {

        int rs = storeCanteenMapper.insert(storeCanteen);

        if (rs < 1 ){
            throw new BusinessException(ReplayCode.DO_ERR);
        }
        return ReplayUtil.success();
    }

    @Override
    public Replay del(int id) {
        // TODO 删除待完成
        return ReplayUtil.success();
    }

    @Override
    public Replay update(StoreCanteen storeCanteen) {

        if (storeCanteen.getCanteenid() == null || storeCanteen.getCanteenid().equals("")){
            throw new BusinessException(101,"必填参数不能为空");
        }

        StoreCanteenExample storeCanteenExample = new StoreCanteenExample();
        StoreCanteenExample.Criteria criteria = storeCanteenExample.createCriteria();

        criteria.andStoreidEqualTo(storeCanteen.getCanteenid());

        int rs = storeCanteenMapper.updateByExampleSelective(storeCanteen, storeCanteenExample);

        if (rs < 1){
            throw new BusinessException(ReplayCode.DO_ERR);
        }

        return ReplayUtil.success();
    }

    @Override
    public Replay select(StoreCanteen storeCanteen, Page page) {

        if (page.isNull()){
            //抛出空值异常
            throw new BusinessException(ReplayCode.Page_NULL);
        }

        StoreCanteenExample storeCanteenExample = new StoreCanteenExample();
        StoreCanteenExample.Criteria criteria = storeCanteenExample.createCriteria();

        List<StoreCanteen> storeCanteens = storeCanteenMapper.selectByExample(storeCanteenExample);
        int count = (int) storeCanteenMapper.countByExample(storeCanteenExample);

        return ReplayUtil.success("查询成功", storeCanteens, count);
    }
}
